package com.haiqi.config.webmagic;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

import java.util.List;

/**
 * Created by hythzx on 2016/8/12.
 */
public class FilmGodSpider implements PageProcessor {

    private Site site = Site.me().setRetryTimes(3).setSleepTime(100);

    @Override
    public void process(Page page) {
        Selectable titleXpath = page.getHtml().xpath("//div[@class='title_all']/h1/text()");
        List<String> all1 = titleXpath.all();
        Selectable rankXpath = page.getHtml().xpath("strong[@class='rank']/text()");
        String s = rankXpath.get();
        System.out.println(s);
        Selectable updatetimeXpath = page.getHtml().xpath("//span[@class='updatetime']/text()");
        String s1 = updatetimeXpath.get();
        System.out.println(s1);
        System.out.println(all1);
        Selectable urlPath = page.getHtml().xpath("//a/@href").regex("ftp:.*");
        List<String> all = urlPath.all();
        System.out.println(all);
    }

    @Override
    public Site getSite() {
        return site;
    }

    public void run(String url){
        Spider.create(this).addUrl(url).thread(5).run();
    }

    public static void main(String[] args) {
        Spider.create(new FilmGodSpider()).addUrl("http://www.dy2018.com/i/97120.html").thread(5).run();
    }
}
